package synchronicity.ui;
import java.util.Date;

import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

import synchronicity.calendar.MockCalendar;
import synchronicity.calendar.PIMCalendar;
import synchronicity.host.BluetoothSchedulerHostFactory;
import synchronicity.host.MeetingSchedulerListener;
import synchronicity.host.SchedulerHost;
import synchronicity.util.Range;
import ui.DisplayContext;
import ui.Screen;


public class SynchronicityDisplayContext extends MIDlet implements
		DisplayContext, MeetingSchedulerListener {
	
	private Screen currentScreen;
	
	private Screen lastScreen;
	
	private SchedulerHost host;

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {
		host = BluetoothSchedulerHostFactory.getInstance().createSchedulerHost( PIMCalendar.getInstance() );
		host.addMeetingSchedulerListener(this);
		setScreen(HomeScreen.getInstance(this));
	}

	public Display getDisplay() {
		return Display.getDisplay(this);
	}

	public Screen getPreviousScreen() {
		return lastScreen;
	}

	public void setScreen(Screen screen) {
		lastScreen = currentScreen;
		currentScreen = screen;
		currentScreen.enter();
	}
	
	/**
	 * Event fired by the SchedulerHost when a successful meeting has been scheduled.
	 * TODO change screen to show user the result
	 */
	public void meetingScheduled(Range timeRange, String title) {
		System.out.println("Meeting scheduled "+ timeRange+ title);
		Date startDate = new Date(timeRange.getLeft());
		Date endDate = new Date(timeRange.getRight());
		currentScreen.switchScreen(new MeetingScheduledScreen(this,startDate,endDate,title));
	}
	public SchedulerHost getHost() {
		return host;
	}
}
